package org.seed.rpc.core.routing;

import org.seed.rpc.core.RuntimeLocal;

import java.util.Iterator;
import java.util.List;

public class RandomStrategy implements RoutingStrategy {
    @Override
    public Endpoint determineEndpoint(String serviceName) {
        List<String> endpoints = RuntimeLocal.getEndpointList(serviceName);
        if (endpoints == null || endpoints.size() == 0) {
            return null;
        }
        int size = endpoints.size();
        int useIndex = (int) Math.random() * size;
        useIndex = useIndex % size;
        String endpointStr = endpoints.get(useIndex);
        return new Endpoint(endpointStr);
    }

    @Override
    public Endpoint determineEndpointExcept(String serviceName, List<String> excepts) {
        List<String> endpoints = RuntimeLocal.getEndpointList(serviceName);
        if (endpoints == null || endpoints.size() == 0) {
            return null;
        }
        Iterator<String> iterator = endpoints.iterator();
        while (iterator.hasNext()) {
            if (excepts.contains(iterator.next())) {
                iterator.remove();
            }
        }
        if (endpoints.size() == 0) {
            return null;
        }
        int size = endpoints.size();
        int useIndex = (int) Math.random() * size;
        useIndex = useIndex % size;
        String endpointStr = endpoints.get(useIndex);
        return new Endpoint(endpointStr);
    }
}
